# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def trav(root):
if not root:
return [False, False, None]
a = trav(root.left)
b = trav(root.right)
if a[-1] != None:
return a
if b[-1] != None:
return b
arr = [False, False, None]
if root == p:
arr[0] = True
if root == q:
arr[1] = True
arr[0] = arr[0] or a[0] or b[0]
arr[1] = arr[1] or a[1] or b[1]
if arr[0] and arr[1]:
arr[2] = root
return arr
return arr
return trav(root)[2]
1648A - Weird Sum | 427A - Police Recruits |
535A - Tavas and Nafas | 581A - Vasya the Hipster |
1537B - Bad Boy | 1406B - Maximum Product |
507B - Amr and Pins | 379A - New Year Candles |
1154A - Restoring Three Numbers | 750A - New Year and Hurry |
705A - Hulk | 492B - Vanya and Lanterns |
1374C - Move Brackets | 1476A - K-divisible Sum |
1333A - Little Artem | 432D - Prefixes and Suffixes |
486A - Calculating Function | 1373B - 01 Game |
1187A - Stickers and Toys | 313B - Ilya and Queries |
579A - Raising Bacteria | 723A - The New Year Meeting Friends |
302A - Eugeny and Array | 1638B - Odd Swap Sort |
1370C - Number Game | 1206B - Make Product Equal One |
131A - cAPS lOCK | 1635A - Min Or Sum |
474A - Keyboard | 1343A - Candies |